﻿using RTW.Core.Entity;
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc;

namespace RTW.Core.Controllers
{
    public class BaseController:Controller
    {
        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        protected WebResult Success(string msg)
        {
            WebResult apiResult = new WebResult();
            apiResult.success = true;
            apiResult.code = 0;
            apiResult.msg = $"{msg}";
            apiResult.data = "";
            apiResult.count = 0;
            return apiResult;
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="msg">提示信息</param>
        /// <returns></returns>
        protected WebResult Failed(string msg)
        {
            WebResult apiResult = new WebResult();
            apiResult.success = false;
            apiResult.code = (int)ApiEnum.ParameterError; 
            apiResult.msg = $"{msg}";
            apiResult.data = "";
            apiResult.count = 0;
            return apiResult;
        }


        /// <summary>
        /// ajax请求结果
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        protected WebResult MsgResult(bool b)
        {
            if (b)
            {
                return Success("操作成功！"); 
            }
            else
            {
                return Failed("操作失败！");
            }
        }

        /// <summary>
        /// 列表请求结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="total">记录数</param>
        /// <returns></returns>
        protected WebResult ListsResults<T>(IList<T> data,int total) where T : class
        {
            WebResult apiResult = new WebResult();
            apiResult.success = true;
            apiResult.code = 0;
            apiResult.msg = (total >= 0) ? "success" : "failed";
            apiResult.data = data;
            apiResult.count = total;
            return apiResult;
        }

        /// <summary>
        /// 列表请求结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected WebResult ListsResult<T>(IList<T> data) where T : class
        {
            WebResult apiResult = new WebResult();
            apiResult.success = true;
            apiResult.code = 0;
            apiResult.msg = (data.Count >= 0) ? "success" : "failed";
            apiResult.data = data;
            apiResult.count = data.Count;
            return apiResult;
        }


        /// <summary>
        /// 列表请求结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="count">记录数</param>
        /// <returns></returns>
        protected WebResult ListResult<T>(IList<T> data) where T : class
        {
            WebResult apiResult = new WebResult();
            apiResult.success = true;
            apiResult.code = 0;
            apiResult.msg = (data.Count>=0)?"success":"failed";
            apiResult.data = data;
            apiResult.count = data.Count;
            return apiResult;
        }

        /// <summary>
        /// 数据请求结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns></returns>
        protected WebResult DataResult(dynamic data)
        {
            WebResult apiResult = new WebResult();
            apiResult.success = true;
            apiResult.msg = "";
            apiResult.data = data;
            apiResult.code = (int)ApiEnum.Status;
            return apiResult;
        }

        /// <summary>
        /// 获取缩略图路径
        /// </summary>
        /// <param name="imageUrl">图片地址</param>
        /// <param name="thumbName">缩略图标识</param>
        /// <returns></returns>
        protected string GeThumbImage(string imageUrl, string thumbName)
        {
            if (string.IsNullOrWhiteSpace(imageUrl) || string.IsNullOrWhiteSpace(thumbName))
                return string.Empty;

            return string.Format(imageUrl, thumbName);
        }
    }
}
